<?php

class Model_App {

    private static $name = "application";
    public static $APPID = "appId";
    public static $API_KEY = "apiKey";
    public static $SECRET_KEY = "secretKey";
    public static $HOLDER = "holder";

    public function getAllApp() {
        $sql = "SELECT * FROM " . self::$name;
        $db = Model_Connections::getInstance()->getDbConnect();
        $data = $db->fetchAll($sql);
        return $data;
    }

    public function insertApp($appId, $apiKey, $secretKey, $holder) {
        if (isset($appId) && $appId != null
                && isset($apiKey) && $apiKey != null
                && isset($secretKey) && $secretKey != null
                && isset($holder) && $holder != null) {

            $query = sprintf("INSERT INTO `" . self::$name . "`(`" . self::$APPID . "`, `" . self::$API_KEY . "`,`" . self::$SECRET_KEY . "`,`" . self::$HOLDER . "`)
            VALUES ('%d','%s','%s','%s')"
                    , mysql_real_escape_string($appId)
                    , mysql_real_escape_string($apiKey)
                    , mysql_real_escape_string($secretKey)
                    , mysql_real_escape_string($holder));
            $db = Model_Connections::getInstance()->getDbConnect();
            return $db->query($query);
        }
        return null;
    }

    public function getSECRECTByAPIKEY($apikey) {
        $sql = "SELECT " . self::$SECRET_KEY . " FROM " . self::$name . " where " . self::$API_KEY . " = '" . $apikey. "'";
        $db = Model_Connections::getInstance()->getDbConnect();
        $data = $db->fetchAll($sql);
        if ($data != null)
            return $data[0];
        return $data;
    }

    public function getAppIdByAPIKEY($apikey) {
        $sql = "SELECT " . self::$APPID . " FROM " . self::$name . " where " . self::$API_KEY . " = " . $apikey;
        $db = Model_Connections::getInstance()->getDbConnect();
        $data = $db->fetchAll($sql);
        if ($data != null)
            return $data[0];
        return $data;
    }

    public function getAPIKEYByAppId($id) {
        $sql = "SELECT " . self::$API_KEY . " FROM " . self::$name . " where " . self::$APPID . " = " . $id;
        $db = Model_Connections::getInstance()->getDbConnect();
        $data = $db->fetchAll($sql);
        if ($data != null)
            return $data[0];
        return $data;
    }

    public function getSECRECTKEYByAppId($id) {
        $sql = "SELECT " . self::$SECRET_KEY . " FROM " . self::$name . " where " . self::$APPID . " = " . $id;
        $db = Model_Connections::getInstance()->getDbConnect();
        $data = $db->fetchAll($sql);
        if ($data != null)
            return $data[0];
        return $data;
    }

    public function deleteApp($id) {
        if (isset($id) && $id != null) {
            $sql = sprintf("DELETE FROM " . self::$name . " WHERE " . self::$APPID . " = '%d' ", mysql_real_escape_string($id));
            $db = Model_Connections::getInstance()->getDbConnect();
            $data = $db->query($sql);
            return $data;
        }
        return null;
    }

}

?>
